package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
public class orderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Override
    public void importOrderSettings(List<OrderSetting> orderSettings) {

        for (OrderSetting orderSetting : orderSettings) {
            //在批量导入数据时，判断是否之前导入过
            Integer count = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());

            if (count > 0) {
                //如果已经导入过数据，就执行更新
                orderSettingMapper.updateNumberByOrderDate(orderSetting);
            } else {
                //如果没有导入过，就执行新增
                orderSettingMapper.add(orderSetting);

            }
        }

    }

    /*@Override
    public List<Map> findOrderSettingByMonth(String date) {

        //定义月首
        String dateBegin = date + "-01";
        //定义月末
        String dateEnd = date + "-31";
        //通过月份查询当前页数据
        List<OrderSetting> list = orderSettingMapper.findOrderSettingByMonth(dateBegin,dateEnd);

        List<Map> maps = new ArrayList<>();
        //循环遍历所有的ordersetting对象重新封装成固定的格式。
        for (OrderSetting orderSetting : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("date",orderSetting.getOrderDate().getDate());
            map.put("number",orderSetting.getNumber());
            map.put("reservations",orderSetting.getReservations());
            maps.add(map);
        }

        return maps;
    }*/

    @Override
    public List<Map> findOrderSettingByMonth(String date) {

        //定义月首
        String dateBegin = date + "-01";
        //定义月末
        String dateEnd = date + "-31";
        //通过月份查询当前页数据
        List<Map> list = orderSettingMapper.findOrderSettingByMonth(dateBegin, dateEnd);

        return list;
    }

    /**
     * 通过日期修改可预约数量
     *
     * @param orderSetting
     */
    @Override
    public void updateNumberByOrderDate(OrderSetting orderSetting) {

        //通过日期查询该记录是否已存在
        Integer count = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
        if (count > 0) {
            //如果已存在，执行修改
            orderSettingMapper.updateNumberByOrderDate(orderSetting);

        } else {
            //如果不存在，就执行新增
            orderSettingMapper.add(orderSetting);
        }

    }


    /**
     * 清理预约设置数据
     *
     * @param monthEnd
     */
    @Override
    public void clearByMonth(String monthEnd) {
        orderSettingMapper.clearByMonth(monthEnd);
    }
}
